package GUI;



import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.CharArrayWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.JScrollPane;

import client.ClientConsole;

import Library.Histogram;
import Library.LateReport;
/**
 * This class is the late report to the Book
 */

public class GUI_LateReportBook extends JPanel{
	/**
	 * client  Connected
	 */
	private ClientConsole clc;
	/**
	 *  label of the average
	 */
	private JLabel lblavg;
	/**
	 * label of the result average
	 */
	private JLabel lblgetavg;
	/**
	 *  label of the median
	 */
	private JLabel lblmed;
	/**
	 * label of the result median
	 */
	private JLabel lblgetmed;
	/**
	 * class for late loans to book
	 */
	private LateReport lateReport;
	/**
	 * panel for histogram
	 */
	private Histogram h;
	/**
	 * Display statistic info to late loans for book
	 * @param cc
	 * @param gl
	 * @param r
	 * @param lReport
	 */
	public GUI_LateReportBook(ClientConsole cc, final GUI_LogIn gl,final GUI_LateReport r, LateReport lReport)
	{
		clc=cc;
		this.setSize(700,600);
		setLayout(null);
		

		this.lateReport = lReport;
		JLabel lblLateInLoan = new JLabel("Late In Loan Report");
		lblLateInLoan.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblLateInLoan.setBounds(275, 11, 181, 26);
		add(lblLateInLoan);

		lblavg = new JLabel("Average: ");
		lblavg.setBounds(22, 96, 136, 23);
		add(lblavg);
		
		lblgetavg = new JLabel(lateReport.getAvg().toString());
		lblgetavg.setBounds(122, 96, 136, 23);
		add(lblgetavg);
		
		lblmed = new JLabel("Median: ");
		lblmed.setBounds(22, 120, 136, 23);
		add(lblmed);
		
		lblgetmed = new JLabel(lateReport.getMed().toString());
		lblgetmed.setBounds(122, 120, 136, 23);
		add(lblgetmed);
		
		JButton btnHistogram = new JButton(" show histogram ");
		btnHistogram.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				ArrayList<Long> list=lateReport.lateReportList;
				if(list.size()>0)
				{
				Long sizeArr=(list.get(list.size()-1));
				int sizeArr1=Integer.parseInt(sizeArr.toString());
				String [] s =new String[sizeArr1+1] ;
				
				for (Integer i=0;i<sizeArr1+1;i++)
					s[i]=i.toString();
				
				int [] val =new int[sizeArr1+1];
				
				for (Integer i=0;i<list.size();i++)
				{
					int v =Integer.parseInt(list.get(i).toString());
					val[v]++;
				}
               
               
                for (Integer i=0;i<sizeArr1+1;i++)
				{
                	val[i]=val[i]*10;
				}
                int max=val[0];
                for (Integer i=0;i<sizeArr1+1;i++)
				{
					if(max<val[i])
						max=val[i];
				}
				h=new Histogram(s,val,max/5,100);
				int flg = h.his();
				if (flg==0)
					JOptionPane.showMessageDialog(new JFrame(),"Not enough data");
				}
				else
					JOptionPane.showMessageDialog(new JFrame(),"No values,can not be displayed Histogrma");
			}
		});
		btnHistogram.setBounds(22, 152, 166, 19);
		add(btnHistogram);
	
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.setContentPane(r);
			}
		});
		btnBack.setBounds(401, 441, 89, 23);
		add(btnBack);
		
		JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.LogOut();
			}
		});
		btnLogOut.setBounds(500, 441, 89, 23);
		add(btnLogOut);
	}
	
}